package facade.open.response;

import facade.vo.base.UserVO;
import models.base.AdminUser;
import models.enums.AvailableStatus;
import play.i18n.Messages;

import java.io.Serializable;

/**
 * 用户信息.
 */
public class AdminUserResponse extends ResponseWithAbility implements Serializable {

    private static final long serialVersionUID = -8908366898420430708L;

    public String id;

    public String loginName;

    public String name;

    public String email;

    public String mobile;

    public CorpResponse corp;

    public String corpId;
    public String corpName;

    public String session;

    public String[] roles;

    public AvailableStatus status;

    /**
     * 状态显示名
     */
    public String statusLabel;


    /**
     * 列表型读取的信息.
     */
    public static AdminUserResponse from(AdminUser user) {
        AdminUserResponse response = fromSimple(user);
        if (response.mobile == null) {
            response.mobile = "-";
        }
        if (user.corp != null ) {
            response.corp = CorpResponse.from(user.corp);
            response.corpName = user.corp.name;
        }
        return response;
    }


    public static AdminUserResponse fromSimple(UserVO user) {
        AdminUserResponse response = AdminUserResponse.fromVerySimple(user);
        if (user != null) {
            response.status = user.status;
            response.statusLabel = Messages.get("userStatus." + user.status);
        }
        return response;
    }

    public static AdminUserResponse fromSimple(AdminUser user) {
        AdminUserResponse response = AdminUserResponse.fromVerySimple(user);
        if (user != null) {
            response.status = user.status;
            response.statusLabel = Messages.get("userStatus." + user.status);
        }
        return response;
    }

    public static AdminUserResponse fromVerySimple(UserVO user) {
        AdminUserResponse response = new AdminUserResponse();
        if (user != null) {
            response.id = user.id;
            response.loginName = user.loginName;
            response.name = user.name;
            response.mobile = user.mobile;
            response.email = user.email;
        }
        return response;
    }

    public static AdminUserResponse fromVerySimple(AdminUser user) {
        AdminUserResponse response = new AdminUserResponse();
        if (user != null) {
            response.id = user.id;
            response.loginName = user.loginName;
            response.name = user.name;
            response.mobile = user.mobile;
            response.email = user.email;
        }
        return response;
    }

    /**
     * 用于公共显示的简单信息
     *
     * @param user
     * @return
     */
    public static AdminUserResponse fromWithSimpleInfo(AdminUser user) {
        if (user == null) {
            return null;
        }
        AdminUserResponse response = new AdminUserResponse();
        response.id = user.id;
        response.name = user.name;
        if (user.corp != null) {
            response.corpId = user.corp.id;
            response.corpName = user.corp.name;
        }

        return response;
    }


    @Override
    public String toString() {
        return "AdminUserResponse{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", mobile='" + mobile + '\'' +
                ", corpName='" + corpName + '\'' +
                '}';
    }
}
